package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.admin.SetmealDishMapper;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.pojo.Setmeal;
import com.sky.pojo.SetmealDish;
import com.sky.result.PageResult;
import com.sky.service.admin.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐 业务层 实现类
 *
 * @Author mr.li
 * @Date 2025/7/8 21:21
 */

@Service
public class SetmealServiceImpl implements SetmealService {


    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;
    /**
     * 新增套餐
     *
     * @param setmealDTO 套餐信息
     * @return
     */
    @Override
    @Transactional(rollbackFor =Exception.class)
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        setmealMapper.save(setmeal);
        //取出套餐关系
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        if (!CollectionUtils.isEmpty(setmealDishes)){
            setmealDishes.forEach(s->{s.setSetmealId(setmeal.getId());});

            setmealDishMapper.save(setmealDishes);

        }
    }
    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO   分页请求参数
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        List<Setmeal> setmealList =setmealMapper.pageQuery(setmealPageQueryDTO);
        Page<Setmeal> p = (Page<Setmeal>)   setmealList;
        return new PageResult(p.getTotal(),p.getResult());
    }

    /**
     * 删除套餐
     *
     * @param ids 要删除的套餐id集合
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByIds(List<Integer> ids) {

        //1. 首先起售的商品无法删除
        //1.1 根据id查询setmeal套餐的状态 ，如果有一个起售中，就都不能删除

        for (Integer id : ids) {

            SetmealVO setmealVO= setmealMapper.getById(id);
            if (setmealVO.getStatus()==1){
                //抛异常
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }



        //2.根据id  删除套餐菜品关系表中的数据和  套餐表的数据
        setmealMapper.deleteByIds(ids);


        setmealDishMapper.deleteSetmealByIds(ids);
        //因为在套餐和菜品两个表中，套餐是主表


    }

    /**
     * 根据id查询套餐
     *
     * @param setmealId 套餐id
     * @return
     */
    @Override
    public SetmealVO getById(Integer setmealId) {
        //sql中实现了多表关联，并封装了返回对象
        SetmealVO setmealVO = setmealMapper.getById(setmealId);

//        //根据套餐的id，获取所有套餐菜品的关系，从而找到菜品id集合//算了联系联系sql语句吧，不这么写了
//        List<SetmealDish> setmealDishes =setmealDishMapper.getById(setmealId);
//        for (SetmealDish setmealDish : setmealDishes) {
//
//
//        }

        return setmealVO;
    }

    /**
     * 修改套餐信息
     *
     * @param setmealDTO 修改后的套餐信息
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        //拷贝  BeanUtils
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        //删除该套餐的菜品，并添加新的菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (!CollectionUtils.isEmpty(setmealDishes)){
//            List<Integer> collect = setmealDishes.stream().map(s -> s.getSetmealId().intValue()).collect(Collectors.toList());
            //为每一个套餐菜品设置值
            List<SetmealDish> setmealDishList = setmealDishes.stream().peek(s -> s.setSetmealId(setmeal.getId())).collect(Collectors.toList());


            setmealDishMapper.deleteSetmealByIds(Arrays.asList(setmeal.getId().intValue()));

            //添加新的套餐菜品
            setmealDishMapper.save(setmealDishList);

        }

    }

    /**
     * 起售停售套餐
     *
     * @param status 状态
     * @param id     套餐id
     * @return
     */
    @Override
    public void startOrStop(Long status, Long id) {

        setmealMapper.startOrStop(status,id);

    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
